package cn.ztr.familyalbum.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.ztr.familyalbum.common.CommonResult;
import cn.ztr.familyalbum.common.ResultCode;
import cn.ztr.familyalbum.dto.GrowthAlbumDocumentsDto;
import cn.ztr.familyalbum.service.IGrowthAlbumDocumentsService;
import cn.ztr.familyalbum.entity.GrowthAlbumDocuments;
import cn.ztr.familyalbum.common.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/growthAlbumDocuments")
@Api(value = "订单页面",tags = "订单操作接口")
public class GrowthAlbumDocumentsController {

    @Autowired
    public IGrowthAlbumDocumentsService growthAlbumDocumentsService;

    /**
    * 保存和修改公用的
    * @param growthAlbumDocuments  传递的实体
    */
    @ApiOperation(value = "新增或修改")
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public CommonResult save(@RequestBody GrowthAlbumDocuments growthAlbumDocuments){
        try {
            if(growthAlbumDocuments.getId()!=null && growthAlbumDocumentsService.selectById(growthAlbumDocuments.getId())!=null){
                growthAlbumDocumentsService.updateById(growthAlbumDocuments);
            }else{
                growthAlbumDocumentsService.insert(growthAlbumDocuments);
            }
            return new CommonResult(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
    * 删除对象信息
    * @param growthAlbumDocuments
    * @return
    */
    @ApiOperation(value = "根据id删除订单")
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    public CommonResult delete(@RequestBody GrowthAlbumDocuments growthAlbumDocuments){
        try {
            growthAlbumDocumentsService.updateById(growthAlbumDocuments);
            return new CommonResult(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @ApiOperation(value = "查询所有订单")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult list(){

        List list = growthAlbumDocumentsService.selectDocumentList();
        if(list != null){
            TableDataInfo tableDataInfo = new TableDataInfo();
            TableDataInfo dataPage = tableDataInfo.getDataPage(list);
            return new CommonResult(ResultCode.SUCCESS,dataPage);
        }
        return new CommonResult(ResultCode.INTERNAL_SERVER_ERROR_DB_UNFIND);
    }


    /**
    * 分页查询数据
    *
    * @param dto 查询对象
    * @return PageList 分页对象
    */
    @ApiOperation(value = "分页条件查询")
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public CommonResult json(@RequestBody GrowthAlbumDocumentsDto dto)
    {
        List growthAlbumDocuments = growthAlbumDocumentsService.selectDocument(dto);
        if(growthAlbumDocuments != null){
            TableDataInfo tableDataInfo = new TableDataInfo();
            TableDataInfo dataPage = tableDataInfo.getDataPage(growthAlbumDocuments);
            return new CommonResult(ResultCode.SUCCESS,dataPage);
        }
        return new CommonResult(ResultCode.INTERNAL_SERVER_ERROR_DB_UNFIND);
    }


    @GetMapping("/detailDownload")
    @ApiOperation(value = "获取订单导出")
    public void detailDownload(@RequestParam Map map, HttpServletResponse response)throws IOException {
        GrowthAlbumDocumentsDto dto = BeanUtil.fillBeanWithMap(map, new GrowthAlbumDocumentsDto(), false);
        growthAlbumDocumentsService.detailDownload(dto,response);
    }
}
